package com.lifo.utils.excel;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2018/5/25 0025.
 */
@Slf4j
public class ExcelReader {
    public static final String SAMPLE_XLSX_FILE_PATH = "C:\\Users\\Administrator\\Desktop\\ddd.xlsx";

    public static void main(String[] args) throws IOException, InvalidFormatException {


    }


    public  static List<ExcelBean>    wrapperExcelData(String excelPath){
// Creating a Workbook from an Excel file (.xls or .xlsx)
        Workbook workbook = null;
        List<ExcelBean> excelBeanList = new ArrayList<>();
        try {
            workbook = WorkbookFactory.create(new File(excelPath));
        } catch (IOException e) {
            log.error("excel文件信息读取异常，==>{}",e.getMessage());
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            log.error("excel 文件格式异常，==>{}",e.getMessage());
            e.printStackTrace();
        }

        // Retrieving the number of sheets in the Workbook
     /*   System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");*/

        /*
           =============================================================
           Iterating over all the sheets in the workbook (Multiple ways)
           =============================================================
        */

        // 1. You can obtain a sheetIterator and iterate over it
        Iterator<Sheet> sheetIterator = workbook.sheetIterator();
/*        System.out.println("Retrieving Sheets using Iterator");*/
        while (sheetIterator.hasNext()) {
            Sheet sheet = sheetIterator.next();
          /*  System.out.println("=> " + sheet.getSheetName());*/
            Iterator<Row> rowIterator =  sheet.iterator();
            while (rowIterator.hasNext()){
                ExcelBean excelBean = new ExcelBean();
                Row row = rowIterator.next();
                excelBean.setCell1(row.getCell(1).toString());
                excelBean.setCell2(row.getCell(2).toString());
                excelBean.setCell3(row.getCell(3).toString());
                excelBean.setCell4(row.getCell(4).toString());
                excelBean.setCell5(row.getCell(5).toString());
                excelBean.setCell6(row.getCell(6).toString());
                excelBean.setCell7(row.getCell(7).toString());
          /*      excelBean.setCell8(row.getCell(8).toString());*/
            /*    excelBean.setCell9(row.getCell(9).toString());
                excelBean.setCell10(row.getCell(10).toString());*/
                excelBeanList.add(excelBean);
            }
        }
        return excelBeanList;
    }


}
